package activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;

import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import Utils.AccountUtils;
import Utils.HelloWordModel;
import adapter.AllyRewardItemAdapter;
import base.BaseActivity;
import dao.AllyRewardMonthItem;
import dao.AllyRewardOutput;
import dao.BaseOutput;
import dao.CustomIdInput;
import event.LoginEvent;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import trade.TradeMonth2Adapter;

public class AllyRewardActivity extends BaseActivity {

    private DelegateAdapter mDelegateAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ally_reward;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
        mDelegateAdapter = new DelegateAdapter(layoutManager);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mDelegateAdapter);
        getData();
    }

    private void getData() {
        showProgressDialog();
        CustomIdInput input = new CustomIdInput();
        input.customId = AccountUtils.getCustomId();

        HelloWordModel.getInstance(this).getAllyReward(input).enqueue(new Callback<BaseOutput<AllyRewardOutput>>() {
            @Override
            public void onResponse(@NonNull Call<BaseOutput<AllyRewardOutput>> call,
                                   @NonNull Response<BaseOutput<AllyRewardOutput>> response) {
                dismissProgressDialog();
                BaseOutput<AllyRewardOutput> body = response.body();
                if (body == null) {
                    return;
                }
                try {
                    if ("401".equals(body.result)) {
                        EventBus.getDefault().post(new LoginEvent());
                    } else  if ("success".equals(body.msg) && body.data != null) {
                        if (body.data.items != null) {
                            showList(body.data.items);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<BaseOutput<AllyRewardOutput>> call, @NonNull Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void showList(List<AllyRewardMonthItem> items) {
        if (mDelegateAdapter == null) {
            return;
        }
        for (AllyRewardMonthItem item : items) {
            TradeMonth2Adapter monthAdapter = new TradeMonth2Adapter(this, item.month);
            mDelegateAdapter.addAdapter(monthAdapter);
            AllyRewardItemAdapter itemAdapter = new AllyRewardItemAdapter(this);
            mDelegateAdapter.addAdapter(itemAdapter);
            itemAdapter.setupData(item.customs);
        }
    }
}
